package com.kitty.game.gm.handler;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.ServerService;
import com.kitty.game.activity.service.shidao.ShiDaoHandler;
import com.kitty.game.config.LoginServer;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.player.PlayerService;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.AccountService;
import com.kitty.mina.message.MessagePusher;
import org.nutz.http.Request;
import org.nutz.http.Sender;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GmShidao implements GMFather {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public boolean handle(Role aaaaa, String content) {
        String[] strings = content.split("##");
        if (SpringUtils.getBean(ServerService.class).getServer().getId() == 10000) {
            // 通知开启跨服试道
            Map<String, Object> params = new HashMap<>();
            params.put("content", content);
            try {
                List<LoginServer> list = SpringUtils.getBean(AccountService.class).queryServer();
                logger.error("线路列表=={}", Json.toJson(list, JsonFormat.compact()));
                for (LoginServer server : list) {
                    Request request = Request.create(server.getUrl() + "/haitang/scl/my/startKuafu", Request.METHOD.POST).setParams(params);
                    Sender.create(request).setTimeout(2000).send();
                }
                SpringUtils.getBean(PlayerService.class).delKuafuRole();
                SpringUtils.getBean(ShiDaoHandler.class).setStartH(Integer.parseInt(strings[0]));
                SpringUtils.getBean(ShiDaoHandler.class).setStartM(Integer.parseInt(strings[1]));
                SpringUtils.getBean(ShiDaoHandler.class).prepareOnceShiDao();
            } catch (Exception e) {
                MessagePusher.pushMessage(aaaaa, new RespNotifyMiscEx("#R开启跨服试道失败！！"));
                logger.error("开启跨服试道失败", e);
                return true;
            }
            MessagePusher.pushMessage(aaaaa, new RespNotifyMiscEx("#R开启跨服试道成功！！"));
            return true;
        }
        SpringUtils.getBean(ShiDaoHandler.class).setStartH(Integer.parseInt(strings[0]));
        SpringUtils.getBean(ShiDaoHandler.class).setStartM(Integer.parseInt(strings[1]));
        SpringUtils.getBean(ShiDaoHandler.class).prepareOnceShiDao();
        MessagePusher.pushMessage(aaaaa, new RespNotifyMiscEx("#R开启试道成功！！"));
        return true;
    }
}
